import { readFileSync } from 'fs';
import path from 'path';
import { name } from '../../package.json' with { type: 'json' };

// 获得Creator主窗口
function getMainWebContents() {
    const windows = require('electron').BrowserWindow.getAllWindows();
    for (let i = 0; i < windows.length; i++) {
        const win = windows[i];
        if (win.webContents.getURL().includes('windows/main.html') || (win.title && win.title.includes('Cocos Creator'))) {
            return win.webContents;
        }
    }
    return;
}

function updateMark() {
    const webContents = getMainWebContents();
    if (webContents) {
        const hackCode = readFileSync(path.join(__dirname, '../res/mark.js'), 'utf-8');
        webContents.executeJavaScript(hackCode);
    }
}

export const methods = {
    open() {
        Editor.Panel.open('xforge');
    },
    wiki() {

    },
    issues() {

    },
    'asset-db:ready'() {
        updateMark();
    }
};

export function load() {
    console.log(`load ${name}`);
}

export function unload() {
    console.log(`unload ${name}`);
}
